﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using KSWSClient.Code.IService;
using KSWSClient.Code.Service;

namespace KSWSClient.ConfigUI
{
    public partial class SOAPSenderPanel : UserControl
    {
        public SOAPSenderPanel()
        {
            InitializeComponent();
        }

        private ISMService smService = new SMService();
        // 重置表单
        private void ResetForm()
        {
            this.txtRequestURL.Text = String.Empty;
            this.txtContent.Text = String.Empty;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            ResetForm();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            ValidateURL(true);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (ValidateURL(false))
            {
                if (!this.txtContent.Text.Trim().Equals(String.Empty))
                {
                    String resultStr = this.smService.SendData(this.txtRequestURL.Text,this.txtContent.Text);
                    MessageBox.Show(this, "Successfully sending!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.txtResponseData.Text = resultStr;
                }
                else
                {
                    MessageBox.Show(this, "Please input the send content data!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }
        // 测试目前的RequestURL里面的URL地址是否可访问
        private bool ValidateURL(bool displaySuccess)
        {
            bool result = false;
            if (!this.txtRequestURL.Text.Trim().Equals(String.Empty))
            {
                result = smService.ValidateURL(this.txtRequestURL.Text);
                if (result)
                {
                    if (displaySuccess)
                    {
                        MessageBox.Show(this, "Successfully to connect the url address!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else
                {
                    MessageBox.Show(this, "Connection problem exsit!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                MessageBox.Show(this, "Please input the url address!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return result;
        }
    }
}
